package com.example.smartcity.ui.person.login

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import com.example.smartcity.R
import com.example.smartcity.databinding.ActivityFeedBackAddBinding
import com.example.smartcity.logic.net_work.Response
import com.example.smartcity.logic.utils.BaseActivity
import com.example.smartcity.logic.utils.Toast.Companion.showToast
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody
import org.json.JSONObject

class FeedBackActivityAdd : BaseActivity() {
    private lateinit var mBinding: ActivityFeedBackAddBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityFeedBackAddBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        title = "反馈内容"
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        // 提交内容
        add()
    }

    @OptIn(DelicateCoroutinesApi::class)
    private fun add() {
        mBinding.FeddBackOK.setOnClickListener {
            // 组装信息发送请求
            GlobalScope.launch {
                getSharedPreferences("token", Context.MODE_PRIVATE).apply {
                    // 获取token
                    Response.getFeedBack(
                        getString("token", "").toString(),
                        RequestBody.create("application/json".toMediaType(), JSONObject().apply {
                            put("content", mBinding.FeedBAckContend.text.toString())
                            put("title", mBinding.FeedBAckTitle.text.toString())
                        }.toString())
                    ).apply {
                        if (this.code == 200){
                            runOnUiThread {
                                "提交成功！".showToast()
                            }
                            finish()
                        }else this.msg.showToast()
                    }
                }
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // 销毁
        finish()
        return super.onOptionsItemSelected(item)
    }
}